package firstServlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class GotoJsp implements Filter {

	// private static final long serialVersionUID = -5376016614476077193L;

	// protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	// throws ServletException, IOException {
	// System.out.println("gotojsp");
	// String jsp = "/welcome.jsp";
	// String password = req.getParameter("password");
	// if (!"1".equals(password)) {
	// jsp = "/login.jsp";
	// } else {
	// req.getSession().setAttribute("p", password);
	// }
	//
	// req.getRequestDispatcher(jsp).forward(req, resp);
	// }
	//
	// protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	// throws ServletException, IOException {
	// this.doGet(req, resp);
	// }

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		System.out.println("filter");
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();
		String password = (String) session.getAttribute("p");
		if (null == password) {
			String uri = req.getRequestURI();
			// 如果请求页是登录页，不转向
			if (uri.equalsIgnoreCase("/bizxingweb/login.jsp")) {
				chain.doFilter(request, response);
			} else {
				res.sendRedirect("login.jsp");
			}
		} else {
			chain.doFilter(request, response);
		}

	}

	public void init(FilterConfig arg0) throws ServletException {

	}
}
